//
//  WBHomeViewController.swift
//  Weibo
//
//  Created by Albert Zhu on 16/9/28.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

/// 原创微博复用ID
private let originalCellId = "originalCellId"
private let retweetedCellId = "retweetedCellId"

class WBHomeViewController: WBBaseViewController {
    
    /// 列表视图模型
    lazy var listViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func loadData() {
        
        refreshControl?.beginRefreshing()
        
        listViewModel.loadStatus(pullup: isPullup) { (isSuccess, hasMorePullup) in
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            self.tableView?.reloadData()
            
            if isSuccess {
                self.tabBarItem.badgeValue = nil
                UIApplication.shared.applicationIconBadgeNumber = 0
            }
        }
    }
}

// MARK: 表格数据源方法
extension WBHomeViewController {
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        /// 视图模型
        let viewModel = listViewModel.statusList[indexPath.row]
        let cellId = (viewModel.status.retweeted_status != nil) ? retweetedCellId : originalCellId
        
        // FIXME: - 修改id
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! WBStatusCell
        
        cell.viewModel = viewModel
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = WBBaseViewController()
        vc.view.backgroundColor = UIColor.orange
        
        navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: UI设置
extension WBHomeViewController {
    
    override func setupTableView() {
        super.setupTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(hehe), isBack: false)
        
        //        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellId)
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellId)
        
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        tableView?.separatorStyle = .none
        
        setupNavTitle()
    }
    
    private func setupNavTitle() {
        
        let title = WBNetworkManager.shared.userAccount.screen_name
        let btn = WBTitleBtn(title: title)
        btn.addTarget(self, action: #selector(clickTitlebtn), for: .touchUpInside)
        navItem.titleView = btn
    }
    
    @objc func clickTitlebtn(btn: UIButton) {
        btn.isSelected = !btn.isSelected
    }
    
    @objc func hehe() {
        
    }
}
